package cn.lyjuan.dwz_manage.core.java.action;


import cn.lyjuan.dwz_manage.core.base.pojo.ActionGroupPojo;
import cn.lyjuan.dwz_manage.core.base.pojo.ActionPojo;
import cn.lyjuan.dwz_manage.core.base.pojo.ManagePojo;
import cn.lyjuan.dwz_manage.core.java.cst.TypeCst;
import cn.lyjuan.dwz_manage.core.java.dto.QueryResultBean;
import cn.lyjuan.dwz_manage.core.java.service.ActionGroupService;
import cn.lyjuan.dwz_manage.core.java.service.ActionService;
import cn.lyjuan.dwz_manage.core.java.service.ManageService;
import cn.lyjuan.dwz_manage.core.java.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by ly on 2015/1/16.
 */
@Controller
@RequestMapping("action/")
public class ActionAction extends BaseAction
{
    @Autowired
    @Qualifier("mgActionService")
    private ActionService actionSer;

    @Autowired
    @Qualifier("mgRoleService")
    private RoleService roleSer;
    
    @Autowired
    @Qualifier("mgManageService")
    private ManageService manageSer;

    @Autowired
    @Qualifier("mgActionGroupService")
    private ActionGroupService actionGroupSer;

    /**
     * 分页检索请求信息
     * @param groupid
     * @param model
     * @return
     */
    @RequestMapping("actionList")
    public void actionList(Integer groupid, Model model)
    {
        ManagePojo manage = manageSer.queryManageByName(this.getUserName());
        
        Map<String, String> param = this.paramsToMap();

        QueryResultBean<ActionPojo> bean = actionSer.queryActions(manage, param, this.getDwzPn(), this.getDwzPs());

        this.pkgManagePage(bean.getTotalCount());
        if (null != groupid)
        {
            ActionGroupPojo group = actionGroupSer.queryGroupById(groupid);
            model.addAttribute("group", group);
        }

        model.addAttribute("actions", bean.getList());
    }

    /**
     * 添加请求页面
     * @return
     */
    @RequestMapping("preAddAction")
    public void preAddAction(Integer groupid, Model model)
    {
        if (null != groupid && groupid > 0)
        {
            ActionGroupPojo group   = actionGroupSer.queryGroupById(groupid);
            model.addAttribute("group", group);
        }
    }

    /**
     * 添加请求
     * @return
     */
    @RequestMapping("addAction")
    @ResponseBody
    public void addAction(ActionPojo action)
    {
        actionSer.addAction(action);

        this.returnDwzAjaxSucc();
    }

    /**
     * 修改请求页面
     * @return
     */
    @RequestMapping("preUpdateAction")
    public void preUpdateAction(HttpServletRequest req, int id)
    {
        // 请求
        ActionPojo action = actionSer.queryActionById(id);

        // 请求组
        ActionGroupPojo group = actionGroupSer.queryGroupById(action.getGroupid());

        req.setAttribute("action", action);
        req.setAttribute("group", group);
    }

    /**
     * 修改请求
     * @return
     */
    @RequestMapping("updateAction")
    @ResponseBody
    public void updateAction(ActionPojo action)
    {
        actionSer.updateAction(action);

        this.returnDwzAjaxSucc();
    }

    /**
     * 删除请求
     * @return
     */
    @RequestMapping("lock")
    @ResponseBody
    public void lockAction(int id)
    {
        actionSer.updateStatus(id, TypeCst.Status.NO);

        this.returnDwzAjaxSucc();
    }

    @RequestMapping("unlock")
    @ResponseBody
    public void unlockAction(int id)
    {
        actionSer.updateStatus(id, TypeCst.Status.OK);

        this.returnDwzAjaxSucc();
    }
}
